package models

import (
	"github.com/astaxie/beego/orm"
	"strconv"
)

type UploadTypeModels struct{}

type UploadType struct {
	Id    int64  `orm:"pk;auto;"`
	Title string `orm:"size(60)"`
	Pid   int64  `orm:"default(0)"`
	Sort  int    `orm:"default(100)"`
}

func init() {
	orm.RegisterModel(new(UploadType))
}

func (u *UploadTypeModels) Get(id int64) (UploadType, error) {
	ut := UploadType{Id: id}
	o := orm.NewOrm()
	err := o.Read(&ut)
	return ut, err
}

func (u *UploadTypeModels) Query() ([]UploadType, error) {
	var list []UploadType
	o := orm.NewOrm()
	_, err := o.QueryTable("upload_type").All(&list)
	return list, err
}

func (u *UploadTypeModels) Insert(maps map[string]interface{}) (int64, error) {
	ut := UploadType{}
	if maps["title"] != nil {
		ut.Title = maps["title"].(string)
	}
	if maps["sort"] != nil {
		ut.Sort, _ = strconv.Atoi(maps["sort"].(string))
	}
	if maps["pid"] != nil {
		ut.Pid, _ = strconv.ParseInt(maps["pid"].(string), 10, 64)
	}
	o := orm.NewOrm()
	return o.Insert(&ut)
}

func (u *UploadTypeModels) Update(maps map[string]interface{}) (int64, error) {
	ut := UploadType{}
	ut.Id, _ = strconv.ParseInt(maps["id"].(string), 10, 64)
	o := orm.NewOrm()
	_ = o.Read(&ut)
	if maps["title"] != nil {
		ut.Title = maps["title"].(string)
	}
	if maps["sort"] != nil {
		ut.Sort, _ = strconv.Atoi(maps["sort"].(string))
	}
	return o.Update(&ut)
}

func (u *UploadTypeModels) Delete(id int64) (int64, error) {
	o := orm.NewOrm()
	return o.Delete(&UploadType{Id: id}, "Id")
}
